<?php

namespace App\Http\Controllers\platform;

use App\Http\Controllers\Controller;
use App\Models\Live;
use Illuminate\Http\Request;

class LiveController extends Controller
{
    
    public function info(Request $Request)
    {
        $data = Live::find($Request->id);
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function push(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Live::find($Request->id);
        if(!$data){
            return response()->json(['code' => '403', 'msg' => '直播间不存在']);
        }
        if($data->is_push == 1){
            return response()->json(['code' => '403', 'msg' => '每个直播只能推流一次']);
        }
        $data->is_push = 1;
        $data->save();
        return response()->json(['code' => '200']);
    }

    public function edit(Request $Request)
    {
        if(!$Request->title){
            return response()->json(['code' => '403', 'msg' => '请输入直播名称']);
        }
        if(!$Request->length){
            return response()->json(['code' => '403', 'msg' => '请选择直播时长']);
        }
        if(!$Request->synopsis){
            return response()->json(['code' => '403', 'msg' => '请输入直播简介']);
        }
        if(!$Request->start_time){
            return response()->json(['code' => '403', 'msg' => '请选择直播时间']);
        }
        if(!$Request->cover_pc){
            return response()->json(['code' => '403', 'msg' => '请上传电脑端封面']);
        }
        if(!$Request->cover_mobile){
            return response()->json(['code' => '403', 'msg' => '请上传手机端封面']);
        }
        
        $data = new Live();
        if($Request->id){
            $data = Live::find($Request->id);
        }else{
            $time = $Request->start_time + $Request->length * 3600;
            $liveNum = uniqid();
            $data->room_number = $liveNum;
            $data->liveNum = $liveNum;
            $data->livePush = $this->getPushUrl($liveNum,$time);
            $data->is_push = 0;
            $data->create_time = time();
        }
        
        $max = Live::orderBy('live_uniqid','desc')->first();
        $live_uniqid = $max->live_uniqid + 1;
        $data->title = $Request->title;
        $data->length = $Request->length;
        $data->synopsis = $Request->synopsis;
        $data->start_time = $Request->start_time;
        $data->cover_pc = $Request->cover_pc;
        $data->cover_mobile = $Request->cover_mobile;
        $data->live_uniqid = $live_uniqid;
        $data->content = $Request->content ?? '';
        $data->notice_info = $Request->notice_info ?? '';
        $data->eid = $Request->eid;
        $data->uid = $Request->uid;
        $data->credit = 0;
        $data->type = 0;
        $data->grade = 1;
        $data->is_sell = $Request->is_sell;
        $data->teach_name = '';
        $data->play_url = '';
        $data->teach_synopsis = '';
        $data->teach_head = '';
        
        $data->retail_price = 0;
        $data->group_purchase_price = 0;
        
        $data->notice_info = '';
        $data->update_time = time();
        $data->sid = 0;
        $data->grouping = 0;
        $data->save();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
    /**
    * 获取推流地址
    * 如果不传key和过期时间，将返回不含防盗链的url
    * @param domain 您用来推流的域名
    *        streamName 您用来区别不同推流地址的唯一流名称
    *        key 安全密钥
    *        time 过期时间 sample 2016-11-12 12:00:00
    * @return String url
    */
    public function getPushUrl($streamName,$time){
        $key = env('LIVEKEY');
        $time = Date("Y-m-d H:i:s",$time);
        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            $txSecret = md5($key.$streamName.$txTime);
            $ext_str = "?".http_build_query(array(
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
            ));
        }
        
        return "webrtc://livepush.xinzhuvip.com/live/".$streamName . (isset($ext_str) ? $ext_str : "");
    }


}
